import { _decorator, Collider, Collider2D, Component, Contact2DType, ITriggerEvent, Node, IPhysics2DContact } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EnemyController')
export class EnemyController extends Component {
    //cocos creator 3.0 3D触发器触发事件，需要添加Collider组件，并设置isTrigger为true

    /* protected onEnable(): void {
        this.node.getComponent(Collider).on("onTriggerEnter", this.onTriggerEnter, this)
    }
    protected onDisable(): void {
        this.node.getComponent(Collider).off("onTriggerEnter", this.onTriggerEnter, this)
    }
    protected onTriggerEnter(event: ITriggerEvent) {
        console.log("onTriggerEnter" + event.selfCollider.node.name + "触发了" + event.otherCollider.node.name + "的触发器");
    } */

    //cocos creator 3.0 2D碰撞器触发事件，需要添加Collider2D组件，并设置Sensor为true
    protected onEnable(): void {
        this.node.getComponent(Collider2D).on(Contact2DType.BEGIN_CONTACT, this.onTriggerEnter, this)
    }
    protected onDisable(): void {
        this.node.getComponent(Collider2D).off(Contact2DType.BEGIN_CONTACT, this.onTriggerEnter, this)
    }
    protected onTriggerEnter(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        console.log("onTrigger2DEnter" + selfCollider.node.name + "触发了" + otherCollider.node.name + "的触发器");
    }

}


